class HomeWork(object):
    """
    作业类
    """

    def decode_and_encode(self):
        """
        字符串的编码和解码
        """
        en_string = '字符串'
        en_string = en_string.encode('utf-8')  # 编码
        de_string = en_string.decode('utf-8')  # 解码
        return en_string, de_string

    def str_crud(self):
        """
        字符串crud
        """
        create_str = '字符串'
        create_str += '创建'

        index_ = create_str[2]  # 索引字符
        find_str = create_str.find('创')  # 获取目标字符索引值

        startswith_str = '开始字符'
        print(startswith_str.startswith('开'))  # True

        endswith = '结束字符'
        print(endswith.endswith('符'))  # True

        replace_str = '替换&字符'
        replace_str = replace_str.replace('&', '')
        split_str = '切割*字符'
        split_str = split_str.split('*')

        join_str = ','.join(split_str)  # 拼接

        strip_str = '   删除前后空格   '
        strip_str = strip_str.strip()

        return create_str, find_str, replace_str, split_str, strip_str, index_

    def str_format(self):
        """
        字符串格式化
        """
        str_format = '格式化'
        format_result = 'format方法{}'.format(str_format)

        index_format = 'index format {0}'.format(str_format)

        args_format = 'args foramt {str_format}'.format(str_format=str_format)

        variable_foramt = f'变量方式{str_format}'

        num = 520.1314326
        num_format = '{:.4f}'.format(num)

        return format_result, index_format, args_format, variable_foramt, num_format

    def save_content(self):
        """
        保存文件
        """
        f = open('week2/content.txt', 'w', encoding='utf-8')
        content = """
            def str_format(self):
                # 字符串格式化
                str_format = '格式化'
                format_result = 'format方法{}'.format(str_format)

                index_format = 'index format {0}'.format(str_format)

                args_format = 'args foramt {str_format}'.format(str_format=str_format)

                variable_foramt = f'变量方式{str_format}'

                num = 520.1314326
                num_format = '{:.4f}'.format(num)
                
                return format_result, index_format, args_format, variable_foramt, num_format
        """
        f.write(content)
        f.close()
        print('写入文件成功!')


if __name__ == "__main__":
    hw = HomeWork()
    hw.decode_and_encode()
    hw.str_format()
    hw.str_format()
    hw.save_content()
